/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.mindlistening.freemind;

import com.googlecode.mindlistening.ConversationControllerImpl;
import com.googlecode.mindlistening.gui.AudioControlPanel;
import com.googlecode.mindlistening.upload.CouchTagsUploader;
import com.googlecode.mindlistening.util.ClasspathHacker;
import freemind.controller.MapModuleManager.MapModuleChangeObserver;
import freemind.modes.MindMapNode;
import freemind.modes.Mode;
import freemind.modes.mindmapmode.MindMapController;
import freemind.modes.mindmapmode.hooks.MindMapHookAdapter;
import freemind.view.MapModule;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JSplitPane;

/**
 * 
 * @author ryan
 */
public class MindListeningHook extends MindMapHookAdapter {
    @Override
    public void startupMapHook() {
        super.startupMapHook();
        MindMapController controller = getMindMapController();
        //controller.getController().getMapModuleManager().addListener(new MapModuleChangeObserver() {

        MindMapNode node = getMindMapController().getMap().getRootNode();
        try {
            hackClasspath();
        } catch (IOException ex) {
            Logger.getLogger(MindListeningHook.class.getName()).log(Level.SEVERE, null, ex);
        }

        FreeMindPropertiesStorage p_storage = new FreeMindPropertiesStorage(controller);
        FreeMindNodeDocumentStorage d_storage = new FreeMindNodeDocumentStorage(node, p_storage);
        CouchTagsUploader cu = new CouchTagsUploader();
        Player ap = new Player(controller);
        ConversationControllerImpl c_controller = new ConversationControllerImpl(d_storage, p_storage, cu,ap);
        AudioControlPanel control_panel = new AudioControlPanel(c_controller, p_storage);

        FreemindModeListener modeListener = new FreemindModeListener(c_controller, p_storage);
        controller.getController().getMapModuleManager().addListener(modeListener);



        controller.getFrame().removeSplitPane();
        JSplitPane split_pane = controller.getFrame().insertComponentIntoSplitPane(control_panel);
        split_pane.resetToPreferredSizes();

        // hack to remove the 
        p_storage.storeProperty("use_split_pane", "false");

    }



    private void hackClasspath() throws IOException {
        // hack to get the mp3spi to the root classpath due to the way
        // java audio loads plugins!!!

        ClasspathHacker.addFile(new File("./plugins/mind-listening/mp3spi1.9.4.jar"));
        ClasspathHacker.addFile(new File("./plugins/mind-listening/tritonus_share.jar"));
        ClasspathHacker.addFile(new File("./plugins/mind-listening/jl1.0.jar"));

        ClasspathHacker.addFile(new File("./plugins/mind-listening/jogg-0.0.7.jar"));
        ClasspathHacker.addFile(new File("./plugins/mind-listening/jorbis-0.0.15.jar"));
        ClasspathHacker.addFile(new File("./plugins/mind-listening/vorbisspi1.0.2.jar"));

        String version = System.getProperty("java.version");
        char minor = version.charAt(2);
        if (minor < '6') {
            System.out.println("A JVM of less than 1.6 detected, loading Additional jars");
        }

    }
}
